package com.example.sixtrainingdemo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.sixtrainingdemo.R;
import com.example.sixtrainingdemo.entity.User;

import java.util.List;

public class UserAdapter extends BaseAdapter {

   private List<User> mList;
   private LayoutInflater mLayoutInflater;

   public UserAdapter(List<User> list, Context context) {
      mList = list;
      mLayoutInflater = LayoutInflater.from(context);
   }

   //刷新数据，传入新的数据列表
   public void updateData(List<User> list) {
      mList.clear();
      mList = list;
      notifyDataSetChanged();
   }

   @Override
   public int getCount() {
      return mList.size();
   }

   @Override
   public User getItem(int position) {
      return mList.get(position);
   }

   @Override
   public long getItemId(int position) {
      return position;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      ViewHolder viewHolder;
      if (convertView == null) {
         viewHolder = new ViewHolder();
         convertView = mLayoutInflater.inflate(R.layout.user_item, null);
         viewHolder.usernameText = convertView.findViewById(R.id.user_name);
         viewHolder.userpasswordText = convertView.findViewById(R.id.user_password);
         convertView.setTag(viewHolder);

      } else {
         viewHolder = (ViewHolder) convertView.getTag();
      }
      User user = getItem(position);
      if (user != null) {
         convertView.setTag(R.integer.friend_tag, user);
         viewHolder.usernameText.setText(user.getUserName());
         viewHolder.userpasswordText.setText(user.getUserPassword());
      }
      return convertView;
   }



   private static class ViewHolder {

      TextView usernameText;
      TextView userpasswordText;

   }
}
